<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\validate\RechangeList as validateMessage;
use app\admin\model\RechangeList as modelMessage;
use think\facade\Hook;

class RechangeList extends Base{
    protected $models = null;
    protected $valid = null;
    public function __construct(){
        parent::__construct();
        $this->models = new modelMessage();
        $this->valid = new validateMessage();
    }

    /**
     * @param array $where
     * @param int $limit
     * @param string $order
     * @return \think\Response
     * @throws \think\exception\DbException
     */
    public function index($where=[],$limit=10,$order='-create_time'){
        $where = $this->_where($where);
        $order = $this->_order($order);
        $_column = $this->models->getList($where,$order,$limit,'update_time',true);
        if(!$_column){
            return $this->__e('无数据');
        }
        return $this->__s($_column);
    }

    /**
     * @param int $id
     * @return array|\think\Response
     */
    public function save($id=0){
        $data = request()->post();
        $validate = new validateMessage;
        if(!$validate->check($data)){
            return $this->__e($validate->getError());
        }
        $column = $this->models;
        if($id){
            if(!$column->isUpdate(true)->save($data)){
                return $this->__e('更新失败');
            }
            $data = [
                'aid'=>self::$uid,
                'action'=>"更新【会员充值】，编号：{$id}",
                'sql'=>$column->getLastSql()
            ];
            Hook::listen('admin_log',$data);
            return $this->__s('更新成功');
        }else{
            $result = $column->save($data);
            if(!$result){
                return $this->__e('添加失败');
            }
            $id = $result->id;
            $data = [
                'aid'=>self::$uid,
                'action'=>"添加【会员充值】，编号：{$id}",
                'sql'=>$column->getLastSql()
            ];
            Hook::listen('admin_log',$data);
            return $this->__s('添加成功');
        }
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function delete($id=0){
        if(!$id){
            return $this->__e('缺少id');
        }
        $column = new modelMessage;
        $info = $column->find($id);
        if(!$info){
            return $this->__e('已删除');
        }
        if(!$info->delete()){
            return $this->__e('删除失败');
        }
        $data = [
            'aid'=>self::$uid,
            'action'=>"删除【会员充值】，编号：{$id}",
            'sql'=>$info->getLastSql()
        ];
        Hook::listen('admin_log',$data);
        return $this->__s('删除成功');
    }

    /**
     * @param int $id
     * @return array|\think\Response
     */
    public function edit($id=0){
        if(!$id){
            return $this->__e('缺少id');
        }
        $column = new modelMessage;
        $_column = $column::get($id);
        if(!$_column){
            return $this->__e('没有数据');
        }
        return $this->__s($_column);
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function read($id=0){
        if(!$id){
            return $this->__e('缺少id');
        }
        $column = new modelMessage;
        $_column = $column::get($id);
        if(!$_column){
            return $this->__e('没有数据');
        }
        return $this->__s($_column);
    }

    /**
     * @param int $id
     * @param int $status
     * @return \think\Response
     */
    public function update($id=0,$status=2){
        if(!$id){
            return $this->__e('缺少id');
        }
        $column = new modelMessage;
        $_column = $column::get($id);
        if(!$_column){
            return $this->__e('没有数据');
        }
        $_column->status=$status;
        if(!$_column->save()){
            return $this->__e('修改失败');
        }
        $data = [
            'aid'=>self::$uid,
            'action'=>"修改【会员充值】，编号：{$id}",
            'sql'=>$_column->getLastSql()
        ];
        Hook::listen('admin_log',$data);
        return $this->__s('修改成功');
    }

    /**
     * 排序
     * @param int $id
     * @param int $type
     * @route('admin/banner/set_order','post')
     * @return \think\Response
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function set_order($id=0,$type=1){
        $banner = new modelMessage;
        $result = $banner->setAdSort($id,$type);
        if(!$result){
            return $this->__e('排序失败，请检查选项是否已在第一或最后一项');
        }
        return $this->__s('操作成功');
    }
}